package com.mycase.im.boot.entity.communication;

import com.fasterxml.jackson.annotation.JsonInclude;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author ZhuHJay
 * @Date 2021/8/2 10:17
 */
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class ResponseMessage extends HashMap<String, Object> {

    /** 定义集合中`key`的标签 **/
    private static final String STATUS_TAG = "status";
    private static final String MESSAGE_TAG = "message";
    private static final String DATA_TAG = "data";
    /** 定义状态码 **/
    private static final Integer SUCCESS_CODE = 200;
    private static final Integer FAIL_CODE = 300;
    /** 用来存放数据的集合 **/
    private Map<String, Object> dataMap = new HashMap<>();

    /** 有参构造: 将状态码和信息以及数据封装到map **/
    private ResponseMessage(int code, String message){
        super.put(STATUS_TAG, code);
        super.put(MESSAGE_TAG, message);
        super.put(DATA_TAG, dataMap);
    }

    /** 成功 -> 静态方法, 返回 -> 状态码, 操作消息 **/
    public static ResponseMessage success(String message){
        return new ResponseMessage(SUCCESS_CODE, message);
    }

    /** 成功 -> 静态方法, 返回 -> 状态码, `操作成功` **/
    public static ResponseMessage success(){
        return ResponseMessage.success("操作成功");
    }

    /** 失败 -> 静态方法, 返回 -> 状态码, 错误信息 **/
    public static ResponseMessage error(String message){
        return new ResponseMessage(FAIL_CODE, message);
    }

    /** 失败 -> 静态方法, 返回 -> 状态码, `操作错误` **/
    public static ResponseMessage error(){
        return ResponseMessage.error("操作错误");
    }

    /** map数据集合追加数据, 链式编程 **/
    public ResponseMessage appendData(String key, Object value){
        dataMap.put(key, value);
        return ResponseMessage.this;
    }

}
